package vn.com.atm;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;

import vn.com.atm.database.JSONParser;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class AllATMActivity extends ListActivity{
	private ProgressDialog pDialog;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
   
    ArrayList<HashMap<String, String>> atmList;
 
    // url to get all products list
   
    // JSON Node names
 

 
    // products JSONArray
    JSONArray atm = null;
    ListView lv ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	
    	atmList = new ArrayList<HashMap<String, String>>();
    	setContentView(R.layout.main);
    	
    	new LoadATMList().execute();
    	
    	
    	
    }
    
    class LoadATMList extends AsyncTask<String, String, String>
    {
    	 @Override
         protected void onPreExecute() {
             super.onPreExecute();
             pDialog = new ProgressDialog(AllATMActivity.this);
             pDialog.setMessage("Loading data. Please wait...");
             pDialog.setIndeterminate(false);
             pDialog.setCancelable(false);
             pDialog.show();
         }
    	@Override
		protected String doInBackground(String...  args) {
         
            return null;
		}
    	 protected void onPostExecute(String file_url) {
             pDialog.dismiss();
             runOnUiThread(new Runnable() {
                 public void run() {
                	lv =(ListView)findViewById(android.R.id.list);
             		lv.setAdapter(SearchActivity.adapter);
             		lv.setOnItemClickListener(new OnItemClickListener() {

            			@Override
            			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            					long arg3) {
            			
            			Intent i = 	new Intent(getBaseContext(),TabMain.class);
            			
            			String lat = SearchActivity.adapter.getItem(arg2).lat;
            			String longt = SearchActivity.adapter.getItem(arg2).longt;
            			i.putExtra("Lat", lat);
            			i.putExtra("Long", longt);
            			startActivity(i);
            				
            			}
            		});
                 }
             });
  
         }
    }
     
    @Override
    protected void onStart() {
    	super.onStart();
    	Button bt = (Button)findViewById(R.id.btnBack);
    	bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(AllATMActivity.this, SearchActivity.class));
			}
		});
    }
   
    @Override
    protected void onStop() {
    	
    	super.onStop();
    	
    }
    @Override
    protected void onResume() {
    	SearchActivity.adapter.notifyDataSetChanged();
    	super.onResume();
    }
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	SearchActivity.adapter.clear();
    }
		
}
